<?php

namespace App\Models\Ddaman;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\common\Base;
use Illuminate\Database\Eloquent\SoftDeletes;

class WorkflowLeaveDate extends Base
{
    use HasFactory;

    protected $table = "workflow_leave_date";

    //const CREATED_AT = null;
    //const UPDATED_AT = null;

    protected $fillable = [
        'guid','leave_id','wid','leave_type','uid','type','start_time','end_time','type_2','day','date_title'
    ];


    public $rule = [
        'name' => 'required|string|between:2,100',
        'logo' => 'required',
    ];

    public $editRule = [
        'id' => 'required',
        'name' => 'required|string|between:2,100',
        'logo' => 'required',
    ];

    public $message = [
        'id.required' => '标识必须',
        'name.required' => '分类名称必须填写',
        'logo.required'=>"请上传品牌logo",
    ];

    /*
     * 获取列表
     * */
    public function getList( $param = [] )
    {
        $limit = isset($param['limit']) ? $param['limit'] : 10;
        $where = [];

        if (isset($param['keywords']) && $param['keywords']){
            $keywords = trim($param['keywords']);
            $where[] = ['name','like',"%$keywords%"];
        }

        $field = array_merge(['id'],$this->fillable);
        $list = $this->where($where)->orderBy('id','desc')->select($field)->paginate($limit);

        return $list;

    }

    /*
     * 获取工作列表
     * */
    public static function getWorkList()
    {
        $field = [
            'id','guid','cid','title'
        ];
        $list = WorkflowDesign::select($field)->orderBy('sort','desc')->get();
        return $list;
    }


    /*
     * 保存请假时间
     * */
    public function changes( $data )
    {
        $leave_list = isset($data['leave_date']) && $data['leave_date'] ? $data['leave_date'] : [];
        if ($leave_list)
        {
            foreach ($leave_list as $item)
            {
                $item['type'] = isset($item['type']) ? $item['type'] : '全天';
                if ($item['type'] == "全天"){
                    $item['day'] = 1;
                }else{
                    $item['day'] = 0.5;
                }
                $item = array_merge($data,$item);
                $lDate = stayFill($this->fillable,$item);
                $info = $this->where('wid',$lDate['wid'])->first();
                if ($info){
                    $result = $this->where('id',$info->id)->update($lDate);
                }else{
                    $lDate['guid'] = getGuid();
                    $result = $this->create($lDate);
                }
                if (!$result){
                    $this->error = "更新失败";
                    return false;
                }
            }
        }

        return true;
    }





}
